/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class="audio-player">
    <div class="bg-info" @click="goRouer">
      <img src="../../assets/images/music_bg.png" />
      <div class="music-name">
        <span>{{ oneMusic?.name }}</span>
        <span>{{ oneMusic?.musicName }}</span>
      </div>
    </div>
    <div class="music-icon">
      <img
        v-show="props.deviceType !== 'mobile'"
        src="../../assets/images/play_mode.svg"
      />
      <img
        v-show="props.deviceType !== 'mobile'"
        @click="previousTrack"
        src="../../assets/images/control_left.png"
      />
      <img
        v-show="isPlay === 0"
        @click="playMusic(PLAY)"
        src="../../assets/images/playBack.svg"
      />
      <img
        v-show="isPlay === 1"
        @click="playMusic(PAUSE)"
        src="../../assets/images/play_icon.png"
      />
      <img
        v-show="props.deviceType !== 'mobile'"
        @click="theNextSong"
        src="../../assets/images/control_right.png"
      />
      <img src="../../assets/images/music_icon.svg" />
    </div>
    <div v-show="props.deviceType !== 'mobile'">
      <img src="../../assets/images/core.svg" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { ref, onMounted, defineProps, defineEmits, watch, inject } from "vue";
import { musicListType } from "../../utils/common";
import { useStore } from "vuex";

const store = useStore();
const router = useRouter();
const player = inject<any>("$musicPlayer");
const oneMusic = ref<object>({});
const musicId = ref<number | null>(1);
const isPlay = ref<number>(0);
const PLAY = 1;
const PAUSE = 0;
const musicList = ref<musicListType[]>(store.state.musicList);
const emit = defineEmits<{
  (e: "updateMusic", message: object): void;
}>();
const props = defineProps({
  deviceType: {
    type: String,
    default: "mobile",
  },
  musicId: {
    type: Number,
    default: 1,
  },
  oneMusic: {
    type: Object,
    default: undefined,
  },
});

watch(
  () => props.musicId,
  (newVal) => {
    musicId.value = newVal;
    oneMusic.value = musicList.value[newVal - 1];
  }
);

watch(
  () => props.oneMusic,
  (newVal) => {
    oneMusic.value = newVal;
  }
);

// 播放或暂停
const playMusic = (value: number) => {
  isPlay.value = value;
  if (value === 0) {
    player.pause();
  } else {
    player.play();
  }
};

const goRouer = () => {
  router.push({
    path: "/musicListDetail",
    query: {
      isPlay: isPlay.value,
      musicId: musicId.value,
      isPlaying: isPlay.value,
    },
  });
};

// 上一曲
const previousTrack = () => {
  player.pause();
  updateMusic(-1);
};
// 下一曲
const theNextSong = () => {
  player.pause();
  updateMusic(1);
};

const updateMusic = (num: number) => {
  const index = musicList.value.findIndex((item) => item.id === musicId.value);
  if (
    (num > 0 && index !== musicList.value.length - 1) ||
    (num < 0 && index !== 0)
  ) {
    oneMusic.value = musicList.value[index + num];
    musicId.value = musicList.value[index + num].id;
    emit("updateMusic", {
      musicId: musicId.value,
      getOneMusic: getOneMusic,
    });
  }
  return;
};

const getOneMusic = (obj: musicListType) => {
  oneMusic.value = obj;
  player.seekTo(0); // 将播放位置重置到开头
  player.play();
  isPlay.value = 1;
};

onMounted(() => {
  const MusicList = sessionStorage.getItem("musicList");
  if (MusicList) {
    musicList.value = JSON.parse(MusicList);
    store.commit("addMusiclist", musicList.value);
  } else {
    musicList.value = store.state.musicList;
  }
  player.seekTo(0);
  player.pause();
  musicId.value = props.musicId;
  oneMusic.value = musicList.value[musicId.value];
});
</script>

<style lang="less" scoped>
@import "./index.less";
</style>